﻿using DingTalk.Api;
using DingTalk.Api.Request;
using DingTalk.Api.Response;
using DingTalkAPIHelp.Base;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DingTalkAPIHelp.Callback
{
    /// <summary>
    /// 回调管理
    /// </summary>
    public class CallbackManager:SysConfigManager
    {
        private static CallbackManager manager = new CallbackManager();
        /// <summary>
        /// 回调管理(单例)
        /// </summary>
        public static CallbackManager Instance
        {
            get
            {
                return manager;
            }
        }

        /// <summary>
        /// 注册回调
        /// </summary>
        /// <param name="url">回调服务地址</param>
        /// <param name="aesKey">数据加密密钥</param>
        /// <param name="token">加解密需要用的token</param>
        /// <param name="tags">需要监听的事件类型事件类型，有20种，“user_add_org”, “user_modify_org”, “user_leave_org”,“org_admin_add”, “org_admin_remove”, “org_dept_create”, “org_dept_modify”, “org_dept_remove”, “org_remove”,“label_user_change”, “label_conf_add”, “label_conf_modify”,“label_conf_del”,“org_change”, “chat_add_member”, “chat_remove_member”, “chat_quit”, “chat_update_owner”, “chat_update_title”, “chat_disband”, “chat_disband_microapp”,“check_in”,“bpms_task_change”,“bpms_instance_change”</param>
        /// <returns></returns>
        public OapiCallBackRegisterCallBackResponse RegCallback(string url,string aesKey,string token,List<string> tags)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/register_call_back");
            OapiCallBackRegisterCallBackRequest request = new OapiCallBackRegisterCallBackRequest();
            request.Url = url;
            request.AesKey = aesKey;
            request.Token = token;
            request.CallBackTag = tags;
            
            OapiCallBackRegisterCallBackResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 查询事件回调接口
        /// </summary>
        public OapiCallBackGetCallBackResponse GetCallback()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/get_call_back");
            OapiCallBackGetCallBackRequest request = new OapiCallBackGetCallBackRequest();
            request.SetHttpMethod("GET");
            OapiCallBackGetCallBackResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 更新事件回调
        /// </summary>
        /// <param name="url">回调服务地址</param>
        /// <param name="aesKey">数据加密密钥</param>
        /// <param name="token">加解密需要用的token</param>
        /// <param name="tags"></param>
        public OapiCallBackUpdateCallBackResponse UpdateCallback(string url, string aesKey, string token, List<string> tags)
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/update_call_back");
            OapiCallBackUpdateCallBackRequest request = new OapiCallBackUpdateCallBackRequest();
            request.Url = url;
            request.AesKey = aesKey;
            request.Token = token;
            request.CallBackTag= tags;

            OapiCallBackUpdateCallBackResponse response = client.Execute(request, accessToken);
            return response;
        }


        /// <summary>
        /// 删除事件回调
        /// </summary>
        public OapiCallBackDeleteCallBackResponse DeleteCalback()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/delete_call_back");
            OapiCallBackDeleteCallBackRequest request = new OapiCallBackDeleteCallBackRequest();
            request.SetHttpMethod("GET");
            OapiCallBackDeleteCallBackResponse response = client.Execute(request, accessToken);
            return response;
        }

        /// <summary>
        /// 获取回调失败的结果列表
        /// </summary>
        /// <returns></returns>
        public OapiCallBackGetCallBackFailedResultResponse GetCallBackFaileds()
        {
            IDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/call_back/get_call_back_failed_result");
            OapiCallBackGetCallBackFailedResultRequest request = new OapiCallBackGetCallBackFailedResultRequest();
            request.SetHttpMethod("GET");
            OapiCallBackGetCallBackFailedResultResponse response = client.Execute(request, accessToken);
            return response;
        }
    }
}
